package com.fancon.android.gardenjokesx.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.entity.Country;

public class Spinners{
	
	@SuppressWarnings("unused")
	private boolean flag = false;
	private Context c;
	private int year, month;
	private String birDay, birMonth, birYear;
	public String getBirDay() {
		return birDay;
	}

	public void setBirDay(String birDay) {
		this.birDay = birDay;
	}

	public String getBirMonth() {
		return birMonth;
	}

	public void setBirMonth(String birMonth) {
		this.birMonth = birMonth;
	}

	public String getBirYear() {
		return birYear;
	}

	public void setBirYear(String birYear) {
		this.birYear = birYear;
	}

	List<String> listCountry = new ArrayList<String>();
	@SuppressWarnings("rawtypes")
	HashMap bloodTypeHashMap = new HashMap();
	@SuppressWarnings("rawtypes")
	HashMap interestedHashMap = new HashMap();
	@SuppressWarnings("rawtypes")
	HashMap genderHashMap = new HashMap();
	@SuppressWarnings("unchecked")
	public Spinners(Context context){
		c = context;
		
		bloodTypeHashMap.put("S", 0);
		bloodTypeHashMap.put("O", 1);
		bloodTypeHashMap.put("A", 2);
		bloodTypeHashMap.put("B", 3);
		bloodTypeHashMap.put("X", 4);
		
		
		
		
		interestedHashMap.put("M", 0);
		interestedHashMap.put("F", 1);
		interestedHashMap.put("A", 2);
	}
	
	@SuppressWarnings("unchecked")
	public Spinners(Context context , View v){
		c = context;
		
		bloodTypeHashMap.put("O", 0);
		bloodTypeHashMap.put("A", 1);
		bloodTypeHashMap.put("B", 2);
		bloodTypeHashMap.put("X", 3);
		bloodTypeHashMap.put("S", 4);
		

		interestedHashMap.put("M", 0);
		interestedHashMap.put("F", 1);
		interestedHashMap.put("A", 2);
		
		
	}
		
	/**
	 * Create Year, month, day spinner 
	 * using methods: createListYear, createListMonth, createListDay.
	 */
	public void createBirthDatePic(Spinner mSpinYear, Spinner mSpinMonth, Spinner mSpinDay, String dateTime){
		
		createListYear(mSpinYear, mSpinMonth, mSpinDay, dateTime);
		
	}
	/**
	 * Create spinner list year from 1900 up to now
	 */
	public void createListYear(Spinner mSpinYear, final Spinner mSpinMonth, final Spinner mSpinDay, final String dateTime){
		//create year array list.
		List<String> listYear = new ArrayList<String>();
		int currentYear;
		String[] date;
		
		Calendar cal = Calendar.getInstance();
		currentYear = cal.get(Calendar.YEAR);
//		currentYear = 2012;
		if(dateTime != null){
			date= dateTime.split("/");
		birMonth = date[0].toString();
		birYear = date[2].toString();
		birDay = date[1].toString();
		}else{
			birYear = ""+1980;
			birMonth = "01";
			birDay = "01";
		}
		
		mSpinYear.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				year = Integer.parseInt( arg0.getItemAtPosition(arg2).toString());
				setBirYear(arg0.getItemAtPosition(arg2).toString());
				createListMonth(mSpinMonth, mSpinDay, birMonth, birDay);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		//get current year.
		//Add years to array list
		for(int i = 1900; i <= currentYear; i ++){
			listYear.add(""+i);
		}
		//create data adapter and add to spinner.
		SpinnerAdapter arrAdapter = new SpinnerAdapter(c,android.R.layout.simple_spinner_item,listYear);
		arrAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		mSpinYear.setAdapter(arrAdapter);
		mSpinYear.setSelection(Integer.parseInt(birYear) - 1900);
		
	}
	
	/**
	 * Create spinner list month
	 */
	public void createListMonth(Spinner mSpinMonth, final Spinner mSpinDay, String birMonth, final String birDay){
		//create month array list.
		List<String> listMonth = new ArrayList<String>();
		mSpinMonth.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				month = Integer.parseInt( arg0.getItemAtPosition(arg2).toString());
				setBirMonth(arg0.getItemAtPosition(arg2).toString());
				createListDay(mSpinDay, birDay);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		//Add month to array list
		String str = "";
		for(int i = 1; i< 13; i ++){
			if(i<10) str = "0"+i;
			else str = ""+i;
			listMonth.add(str);
		}
		//create data adapter and add to spinner.
		SpinnerAdapter arrAdapter = new SpinnerAdapter(c,android.R.layout.simple_spinner_item,listMonth);
		arrAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		mSpinMonth.setAdapter(arrAdapter);
		mSpinMonth.setSelection(Integer.parseInt(birMonth) - 1);
	}
	
	

	/**
	 * Create spinner list year.
	 * @param year: integer, value from 1900 to this year. 
	 * @param month: integer, value from 1 to 12.
	 */
	public void createListDay(Spinner mSpinDay, String birDay){
		
		List<String> listDay = new ArrayList<String>();
		mSpinDay.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				setBirDay(arg0.getItemAtPosition(arg2).toString());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		String str = "";
		// february
		if(month == 2){
			//commonly has 28 days.
			for ( int i = 1; i < 29 ; i++){
				if(i<10) str = "0"+i;
				else str = ""+i;
				listDay.add(str);
			}
			//if year is specially, add day 29th
			if((year % 4) == 0) listDay.add("29");
		}
		// (4, 6, 9, 11) month has 30 days.
		else if (month == 4 || month == 6 || month == 9 || month == 11){
			for ( int i = 1; i < 31 ; i++){
				if(i<10) str = "0"+i;
				else str = ""+i;
				listDay.add(str);
			}
		}
		// other month has 31 days.
		else{
			for ( int i = 1; i < 32 ; i++){
				if(i<10) str = "0"+i;
				else str = ""+i;
				listDay.add(str);
			}
		}
		
		//create array adapter and add to spinner.
		SpinnerAdapter arrAdapter = new SpinnerAdapter(c,android.R.layout.simple_spinner_item,listDay);
		arrAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		mSpinDay.setAdapter(arrAdapter);
		mSpinDay.setSelection(Integer.parseInt(birDay) - 1);
	}
	
	/**
	 * Create gender spinner list with (male,female).
	 */
	public void createListGender(Spinner mSpinGender, String male){
		//create month array list.
		List<String> listGender = new ArrayList<String>();
		mSpinGender.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		//Add gender to array list
		
		listGender.add(c.getResources().getString(R.string.S02_female));
		listGender.add(c.getResources().getString(R.string.S02_male));
		//create data adapter and add to spinner.
		SpinnerAdapter arrAdapter = new SpinnerAdapter(c,android.R.layout.simple_spinner_item,listGender);
		arrAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		mSpinGender.setAdapter(arrAdapter);
		if (male != null) {
			int gender = 0;
			if (male.equals("1")) {
				gender = 1;
			}
			mSpinGender.setSelection(gender);
		}
	}
	
	/**
	 * Create Interested spinner list
	 */
	public void createListInterest(Spinner mSpinInterest, String interested){
		//create month array list.
		List<String> listInterest = new ArrayList<String>();
		mSpinInterest.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		//Add gender to array list
		listInterest.add(c.getResources().getString(R.string.S02_male));
		listInterest.add(c.getResources().getString(R.string.S02_female));
		listInterest.add(c.getResources().getString(R.string.S02_both));
		
		
		
		//create data adapter and add to spinner.
		SpinnerAdapter arrAdapter = new SpinnerAdapter(c,android.R.layout.simple_spinner_item,listInterest);
		arrAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		mSpinInterest.setAdapter(arrAdapter);
		if (interested != null) {
			mSpinInterest.setSelection((Integer)interestedHashMap.get(interested));
			Log.d("index current", (Integer)interestedHashMap.get(interested) + "");
		}
	}
	
	/**
	 * create Country spinner list from array string: countries_array( strings.xml)
	 */
	public void createListCountry(Spinner mSpinCountry, String country, List<Country> listCountries){
		
		List<Country> lstCountry = new ArrayList<Country>();
		lstCountry = listCountries;
		final SpinnerAdapter arrAdapter;
		Log.d("Spinners", "country: "+ country);
		if(country == null || country.equals(c.getResources().getString(R.string.S02_required))){
			Log.d("Spinners", "country code: "+ country);
			listCountry.add(c.getResources().getString(R.string.S02_required));
		}
		for(int i = 0; i< lstCountry.size(); i++){
			listCountry.add(lstCountry.get(i).getCountry_name());
		}
		//create data adapter and add to spinner.
		arrAdapter = new SpinnerAdapter(c,android.R.layout.simple_spinner_item,listCountry);
		arrAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		mSpinCountry.setAdapter(arrAdapter);
		if(country != null){
			if (listCountry.contains(country)) {
				mSpinCountry.setSelection(getCountryId(c, country));
			}
//			flag = false;
		}else {
//			mSpinCountry.setSelection(0);
		}
		

//		mSpinCountry.setOnTouchListener(new OnTouchListener() {
//			
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				// TODO Auto-generated method stub
//				Object o = arrAdapter.getItem(0);
//				if(o.toString().equals(c.getResources().getString(R.string.S02_required))){
//					Log.d("Spinners", "remove item: "+ o.toString());
//						arrAdapter.remove(o.toString());
//						flag = true;
//				}
//				
//				return false;
//			}
//		});
		mSpinCountry.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});
	}
	
	private int getCountryId(Context c, String country){
		int id = 0;
		for(id = 0; id < listCountry.size(); id ++){
			if(country.equals(listCountry.get(id))){
				return id;
			}
		}
		return 0;
	}
	
	/**
	 * Create BloodType spinner list from string array: S02_blood_type( strings.xml)
	 */
	public void createListBloodType(Spinner mSpinBloodType, String bloodType){
		//create month array list.
		mSpinBloodType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		List<String> lstBloodType = new ArrayList<String>();
		String[] str = c.getResources().getStringArray(R.array.S02_bloodType);
        for(int i = 0; i < str.length; i++){
//	           Log.d("Spinners", "array: "+str[i]);
	           lstBloodType.add(str[i]);
        }
		//create data adapter and add to spinner.
		SpinnerAdapter arrAdapter = new SpinnerAdapter(c,android.R.layout.simple_spinner_item, lstBloodType);
		arrAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		mSpinBloodType.setAdapter(arrAdapter);
		if (bloodType != null) {
			mSpinBloodType.setSelection((Integer)bloodTypeHashMap.get(bloodType));
		}
	}
	
	//// Button 
	/**
	 * Check button state
	 * @param c : Context
	 * @param btn : Button
	 * @return state of button: ON(true), OFF( false)
	 */
	public String isBtnOn(Button btn){
		if(btn.getText() == c.getString(R.string.common_On)) return Constant.PUBLIC;
		else return Constant.NOT_PUBLIC;
	}
	
	/**
	 * Get next state of ON/OFF button
	 * @param c
	 * @param btn
	 * @return
	 */
	public String getBtnNextState(Button btn){
		if(btn.getText() == c.getString(R.string.common_On)) return Constant.NOT_PUBLIC;
		else return Constant.PUBLIC;
	}
	
	/**
	 * Set button with appropriate background and text.
	 * @param btn: Button.
	 * @param state: Boolean( ON-true, OFF-false). State to set to button.
	 */
	public void setButtonState(Button btn, String state){
		Drawable d;
		if (state != null) {
			if( state.equals(Constant.PUBLIC)){
				d = c.getResources().getDrawable(R.drawable.common_btn_bg_on);
				btn.setBackgroundDrawable(d);
				btn.setText(c.getString(R.string.common_On));
			}
			else {
				d = c.getResources().getDrawable(R.drawable.common_btn_bg_off);
				btn.setBackgroundDrawable(d);
				btn.setText(c.getString(R.string.common_Off));
			}
		}else{
			d = c.getResources().getDrawable(R.drawable.common_btn_bg_on);
			btn.setBackgroundDrawable(d);
			btn.setText(c.getString(R.string.common_On));
		}
	}
	
	class SpinnerAdapter extends ArrayAdapter<String> {
	    Context context;
	    List<String> items = new ArrayList<String>();
	    private int textSize=12; //initial default textsize  (might be a bit big)

	       public SpinnerAdapter(final Context context, final int textViewResourceId, final List<String> objects) {
	           super(context, textViewResourceId,objects);
	           this.items = objects;
	           this.context = c;

	       }
	       public SpinnerAdapter(final Context context, final int resource, final int textViewResourceId ){
	           super(context, resource, textViewResourceId);
	           String[] str = context.getResources().getStringArray(resource);
	           for(int i = 0; i < str.length; i++){
	        	   items.add(str[i]);
	           }
	       }


	       @Override
	       public View getDropDownView(int position, View convertView,
	               ViewGroup parent) {

	           if (convertView == null) {
	               LayoutInflater inflater = LayoutInflater.from(context);
	               convertView = inflater.inflate(
	                       android.R.layout.simple_dropdown_item_1line, parent, false);
	           }

	           TextView tv = (TextView) convertView
	                   .findViewById(android.R.id.text1);
	           tv.setText(items.get(position).toString());
	           //tv.setTextColor(Color.BLUE);
//	           tv.setTextSize(textSize);
	           return convertView;
	       }

	       @Override
	       public View getView(int position, View convertView, ViewGroup parent) {
	           if (convertView == null) {
	               LayoutInflater inflater = LayoutInflater.from(context);
	               convertView = inflater.inflate(
	                       android.R.layout.simple_spinner_item, parent, false);
	           }

	           // android.R.id.text1 is default text view in resource of the android.
	           // android.R.layout.simple_spinner_item is default layout in resources of android.

	           TextView tv = (TextView) convertView
	                   .findViewById(android.R.id.text1);
	           tv.setText(StringUtil.validString(items.get(position).toString(), 15));
	           //tv.setTextColor(Color.BLUE);
	           tv.setTextSize(textSize);
	           return convertView;
	       }

	           //set the textsize
	       public void setSpinnerTextSize(int size){

	           textSize= size;
	       }

	           //return the textsize
	       public int getSpinnerTextSize(){
	           return textSize;
	       }

	}
}


